﻿Imports NHibernate.Criterion

Public Class PickRepository
    Public Sub SavePickInfo(ByVal pickId As Integer, ByVal teamId As Integer)
        Using mySession = Persister.OpenSession()
            Dim thisPick = mySession.Get(Of PickObj)(pickId)

            If thisPick IsNot Nothing Then
                Dim thisTeam = mySession.Get(Of TeamObj)(teamId)
                thisPick.Team = thisTeam

                Using myTrans = mySession.Transaction
                    myTrans.Begin()
                    mySession.SaveOrUpdate(thisPick)
                    myTrans.Commit()
                End Using
            End If
        End Using
    End Sub

    Public Function FindAllPicks() As IList(Of PickObj)
        Using mySession = Persister.OpenSession()
            Dim crit = mySession.CreateCriteria(GetType(PickObj))
            Return crit.List(Of PickObj)()
        End Using
    End Function

    Public Function FindAllPicksPerRound(ByVal round As Integer) As IList(Of PickObj)
        Using mySession = Persister.OpenSession()
            Dim crit = mySession.CreateCriteria(GetType(PickObj))
            crit.Add(Restrictions.Eq("BracketRound", round))
            Return crit.List(Of PickObj)()
        End Using
    End Function

    Public Function FindAllPicksPerUser(ByVal user As String) As IList(Of PickObj)
        Using mySession = Persister.OpenSession()
            Dim crit = mySession.CreateCriteria(GetType(PickObj))
            crit.Add(Restrictions.Eq("Username", user))
            Return crit.List(Of PickObj)()
        End Using
    End Function

    Public Function FindAllPicksPerUserUpToSpecificRound(ByVal user As String, ByVal maxRound As Integer) As IList(Of PickObj)
        Using mySession = Persister.OpenSession()
            Dim crit = mySession.CreateCriteria(GetType(PickObj))
            crit.Add(Restrictions.Eq("Username", user))
            crit.Add(Restrictions.Le("BracketRound", maxRound))
            Return crit.List(Of PickObj)()
        End Using
    End Function

    Public Function FindUserPicksPerRound(ByVal user As String, ByVal round As Integer) As IList(Of PickObj)
        Using mySession = Persister.OpenSession()
            Dim crit = mySession.CreateCriteria(GetType(PickObj))
            crit.Add(Restrictions.Eq("Username", user))
            crit.Add(Restrictions.Eq("BracketRound", round))
            Dim list = crit.List(Of PickObj)()

            Dim numOfChoices As Integer
            Select Case round
                Case 1
                    numOfChoices = 4
                Case 2
                    numOfChoices = 2
                Case 3
                    numOfChoices = 1
                Case 4
                    numOfChoices = 1
                Case 5
                    numOfChoices = 1
                Case 6
                    numOfChoices = 1
                Case Else
                    numOfChoices = 0
            End Select

            For i = list.Count To numOfChoices - 1
                Dim newPick As New PickObj With {.Username = user, .BracketRound = round}
                mySession.Save(newPick)
                list.Add(newPick)
            Next

            Return list
        End Using
    End Function


End Class
